package cn.sfrog.project.biz.admin.service.impl;

import cn.hutool.json.JSONUtil;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.project.biz.admin.domain.BizServiceOrderDataUpload;
import cn.sfrog.project.biz.admin.domain.vo.BizServiceOrderDataUploadFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizServiceOrderDataUploadQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizServiceOrderDataUploadVO;
import cn.sfrog.project.biz.admin.mapper.BizServiceOrderDataUploadMapper;
import cn.sfrog.project.biz.admin.service.IBizServiceOrderDataUploadService;
import cn.sfrog.project.biz.web.vo.order.AppOrderDataUploadResultVO;
import cn.sfrog.project.biz.web.vo.order.AppOrderDataUploadVO;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 服务订单资料上传Service业务层处理
 */
@Service
public class BizServiceOrderDataUploadServiceImpl extends ServiceImpl<BizServiceOrderDataUploadMapper, BizServiceOrderDataUpload> implements IBizServiceOrderDataUploadService {
    @Resource
    private BizServiceOrderDataUploadMapper bizServiceOrderDataUploadMapper;

    /**
     * 查询服务订单资料上传
     *
     * @param id 服务订单资料上传主键
     * @return 服务订单资料上传
     */
    @Override
    public BizServiceOrderDataUploadVO selectBizServiceOrderDataUploadById(Long id) {
        return bizServiceOrderDataUploadMapper.selectBizServiceOrderDataUploadById(id);
    }

    /**
     * 查询服务订单资料上传列表
     *
     * @param query 服务订单资料上传
     * @return 服务订单资料上传
     */
    @Override
    public List<BizServiceOrderDataUploadVO> selectBizServiceOrderDataUploadList(BizServiceOrderDataUploadQueryVO query) {
        return bizServiceOrderDataUploadMapper.selectBizServiceOrderDataUploadList(query);
    }

    /**
     * 新增服务订单资料上传
     *
     * @param form 服务订单资料上传
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizServiceOrderDataUpload(BizServiceOrderDataUploadFormVO form) {
        BizServiceOrderDataUpload bizServiceOrderDataUpload = new BizServiceOrderDataUpload();
        BeanUtils.copyProperties(form, bizServiceOrderDataUpload);
        bizServiceOrderDataUpload.setCreateTime(new Date());
        bizServiceOrderDataUpload.setCreateBy(SecurityUtils.getUsername());
        bizServiceOrderDataUpload.setUpdateTime(new Date());
        bizServiceOrderDataUpload.setUpdateBy(SecurityUtils.getUsername());
        return bizServiceOrderDataUpload.insert();
    }

    /**
     * 修改服务订单资料上传
     *
     * @param form 服务订单资料上传
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizServiceOrderDataUpload(BizServiceOrderDataUploadFormVO form) {
        BizServiceOrderDataUpload bizServiceOrderDataUpload = new BizServiceOrderDataUpload();
        BeanUtils.copyProperties(form, bizServiceOrderDataUpload);
        bizServiceOrderDataUpload.setUpdateTime(new Date());
        bizServiceOrderDataUpload.setUpdateBy(SecurityUtils.getUsername());
        return bizServiceOrderDataUpload.updateById();
    }

    /**
     * 批量删除服务订单资料上传
     *
     * @param ids 需要删除的服务订单资料上传主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizServiceOrderDataUploadByIds(Long[] ids) {
            return BizServiceOrderDataUpload.DAO.delete(Wrappers.lambdaQuery(BizServiceOrderDataUpload.class).in(BizServiceOrderDataUpload::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        //return BizServiceOrderDataUpload.DAO.update(Wrappers.lambdaUpdate(BizServiceOrderDataUpload.class).in(BizServiceOrderDataUpload::getId, (Object[]) vo.getIds()).set(BizServiceOrderDataUpload::getStatus, vo.getStatus()));
        return true;
    }

    @Override
    public Boolean saveOrderDataUpload(AppOrderDataUploadVO form) {
        BizServiceOrderDataUpload dataUpload = new BizServiceOrderDataUpload();
        dataUpload.setServiceOrderId(form.getServiceOrderId());
        dataUpload.setContent(form.getContent());
        dataUpload.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        if(StringUtils.isNotBlank(form.getImages())){
            String[] imageArr = form.getImages().split(",");
            dataUpload.setImages(JSONUtil.toJsonStr(imageArr));
        }
        return dataUpload.insert();
    }

    @Override
    public List<AppOrderDataUploadResultVO> selectDataUploadListByOrder(Long id) {
        List<AppOrderDataUploadResultVO> dataUploadList = bizServiceOrderDataUploadMapper.selectDataUploadListByOrder(id);
        for (AppOrderDataUploadResultVO vo:dataUploadList) {
            if(StringUtils.isNotBlank(vo.getImages())){
                List<String> imageList = JSONObject.parseArray(vo.getImages(), String.class);
                vo.setImageList(imageList);
            }
        }
        return dataUploadList;
    }
}
